package org.projectwithoutname.ui.pages;

import java.io.IOException;
import java.util.Collections;
import java.util.Date;

import javax.swing.LayoutFocusTraversalPolicy;

import model.Entity;
import model.Season;
import model.Workspace;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Device;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.part.EditorPart;
import org.projectwithoutname.commons.ICommandIds;
import org.projectwithoutname.ui.pages.input.EntityEditorInput;
import org.projectwithoutname.ui.util.ScreenToolkit;
import org.projectwithoutname.ui.util.Separator;

public class SeasonPage extends EditorPart {

	public static final String ID = ICommandIds.SEASON_PAGE; 
	private DateTime begin;
	private DateTime end;
	private Text commentText;
	private Text descriptionText;
	private boolean isDirty = false;
	private Composite composite;
	private Label datePremierLabel;
	private Label dateFinalizedLabel;
	private Label separator;
	private Separator separator2;
	private Label commentsLabel;
	private Separator separator3;
	
	
	

	@Override
	public void doSave(IProgressMonitor monitor) {
		Season season = (Season) getSeasonEditorInput().getEntity();
		season.setPremier(new Date(begin.getYear(), begin.getMonth(), begin.getDay()));
		season.setEnds(new Date(end.getYear(), end.getMonth(), end.getDay()));
		season.setDescription(descriptionText.getText());
		System.out.println("Resource: "+season.eResource());
		try {
			season.eResource().save(Collections.EMPTY_MAP);
		} catch (IOException e) {
			e.printStackTrace();
		}
		setDirty(false);
		initTexts();
		
	}

	@Override
	public void doSaveAs() {
		// TODO Auto-generated method stub
	}

	@Override
	public void init(IEditorSite site, IEditorInput input)
		throws PartInitException{
			setSite(site);
			setInput(input);
			setPartName(getEditorInput().getName());	
		}

	@Override
	public boolean isDirty() {
		return isDirty;
	}

	@Override
	public boolean isSaveAsAllowed() {
		return isDirty;
	}
	
	private void setDirty(boolean dirty){
		isDirty = dirty;
		firePropertyChange(PROP_DIRTY);
	}
	
	@Override
	public void createPartControl(Composite parent){
		composite = parent;
		Color color = new Color(composite.getDisplay(), 122, 153, 194);
		parent.setBackground(color);
		
		
//		init components
		datePremierLabel = new Label(composite, SWT.NONE);
		datePremierLabel.setText("Premiered: ");
		datePremierLabel.setBackground(color);
		
		begin = new DateTime(composite, SWT.BORDER | SWT.READ_ONLY);
		
		dateFinalizedLabel = new Label(composite, SWT.NONE);
		dateFinalizedLabel.setText("Finalized: ");
		dateFinalizedLabel.setBackground(color);
		
		end = new DateTime(composite, SWT.BORDER | SWT.READ_ONLY);

		separator = new Label(composite, SWT.SEPARATOR | SWT.HORIZONTAL);
		
		
		descriptionText = new Text(composite, SWT.WRAP | SWT.V_SCROLL | SWT.MULTI);
		descriptionText.setBackground(color);
		
		separator2 = new Separator(composite, SWT.BORDER_DASH);
		
		commentsLabel = new Label(composite, SWT.NONE);
		commentsLabel.setText("Comments");
		commentsLabel.setBackground(color);
		
		separator3 = new Separator(composite, SWT.BORDER_DASH);
		
		commentText = new Text(composite, SWT.WRAP | SWT.V_SCROLL | SWT.MULTI);
		commentText.setBackground(color);
		
		refresh(composite);
		
		composite.addControlListener(new ControlListener() {
			
			@Override
			public void controlResized(ControlEvent e) {
				refresh(composite);
			}
			
			@Override
			public void controlMoved(ControlEvent e) {
			}
		});
		descriptionText.addKeyListener(new KeyListener()
		{
			@Override
			public void keyPressed(KeyEvent e) {
				setDirty(true);
			}
			
			@Override
			public void keyReleased(KeyEvent e) {
			}
		});
		
		commentText.addKeyListener(new KeyListener()
		{
			@Override
			public void keyPressed(KeyEvent e) {
				isDirty = true;
				firePropertyChange(PROP_DIRTY);
			}
			
			@Override
			public void keyReleased(KeyEvent e) {
			}
		});
	
		composite.setLayout(null);
		
	}
	
	private void refresh(Composite composite){
		int width = composite.getSize().x - 10;
		
		datePremierLabel.setBounds(10, 10, 60, 17);
		begin.setBounds(datePremierLabel.getBounds().width + 15, datePremierLabel.getBounds().y, 80, 20);
		
		dateFinalizedLabel.setBounds(begin.getBounds().width + 100, datePremierLabel.getBounds().y, 60, 17);
		end.setBounds(240, 10, 80, 20);
		
		separator.setBounds(0, datePremierLabel.getBounds().height + 20, width + 10, 2);
		
		descriptionText.setBounds(8, separator.getBounds().height + 50, width, 130);
		
		separator2.setBounds(0, 200, width + 10, 1);
		commentsLabel.setBounds(10, 208, 80, 17);
		separator3.setBounds(0, 228, width + 10, 1);
		
		commentText.setBounds(8, 240, width, 130);
		
		initTexts();
		
	}
	
	private void initTexts() {
		final Device device = this.getSite().getShell().getDisplay(); 
		Color cinza = new Color(device, 156, 156, 156);
		Season season = (Season) getSeasonEditorInput().getEntity();
		
		if (season.getPremier().getDay() != 0){
			begin.setDay(season.getPremier().getDay());
			begin.setMonth(season.getPremier().getMonth());
			begin.setYear(season.getPremier().getYear());
		}
		if (season.getEnds().getDay() != 0){
			end.setDay(season.getEnds().getDay());
			end.setMonth(season.getEnds().getMonth());
			end.setYear(season.getEnds().getYear());
		}
		
//		if(season.getName() == null){
//			textName.setForeground(cinza);	
//			textName.setText("Insira um nome."); 
//		}
//		else textName.setText(season.getName());
//		
		if(season.getDescription() == null){
			descriptionText.setForeground(cinza);	
			descriptionText.setText("Write anything about it.");
		}
		else{
			descriptionText.setText(season.getDescription());
		}
		
		if(season.getComments() == null){
			commentText.setForeground(cinza);	
			commentText.setText("Write anything about it.");
		}
		else commentText.setText(season.getComments());
		
		
		final Color preto = new Color(device, 0, 0, 0);
		
//		textName.addListener(SWT.MouseDown, new Listener(){
//			public void handleEvent(Event event) {
//				if (textName.getText().equals("Insira um nome.")){
//					textName.setText("");
//					textName.setForeground(preto);
//				}	
//			}
//		});
		
		descriptionText.addListener(SWT.MouseDown, new Listener(){
			public void handleEvent(Event event) {
				if (descriptionText.getText().equals("Write anything about it.")){
					descriptionText.setText("");
					descriptionText.setForeground(preto);
				}
			}
		});
		
		commentText.addListener(SWT.MouseDown, new Listener(){
			public void handleEvent(Event event) {
				if (commentText.getText().equals("Write anything about it.")){
					commentText.setText("");
					commentText.setForeground(preto);
				}
			}
		});
	}
	

	private void initCombos(){
		
	}
	
	private EntityEditorInput getSeasonEditorInput() {
		return (EntityEditorInput) getEditorInput();
	}

	@Override
	public void setFocus() {
	}

}
